<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
    </head>
    <body>
        <script>
            /*
            思路：先按照身高进行降序排序，如果身高相同，按照前面多少人进行升序排序。
            定义一个空数组result，然后按照前面多少人进行插入
            */
            var reconstructQueue = function (people) {
                people.sort((a, b) => {
                    if (a[0] != b[0]) {
                        return b[0] - a[0]
                    } else {
                        return a[1] - b[1]
                    }
                })

                let result = []
                for (let i = 0; i < people.length; i++) {
                    //往result对应下标添加people
                    result.splice(people[i][1], 0, people[i])
                }
                return result
            }
            console.log(
                reconstructQueue([
                    [7, 0],
                    [4, 4],
                    [7, 1],
                    [5, 0],
                    [6, 1],
                    [5, 2],
                ]),
            )
        </script>
    </body>
</html>
